"""
    This views determing manager's behaviour:
    view manager determ's default manager's page which allows to confirming or
    not confirming user's timesheet
    view manager_crud determ's additional manager's page which will be allow
    adding new activities and developers to manager's projects
"""

from django.http import HttpResponseRedirect
from django.http import HttpResponse
from django.core.urlresolvers import reverse
from django.shortcuts import render_to_response

from timesheet.models import Time
from timesheet.models import Project

from models import ProjectManagerUser
from models import ProjectManager
from models import ProjectManagerComments

from views_functions.manager_handle_form import HandleManagerForm
from views_functions.manager_crud_form import ProjectUserForm
       

def manager(request, template_manager='manager/manager_timesheet.html'):
    if request.user.is_authenticated():
        manager = request.user
        if request.method == 'POST':
            try:
                form = HandleManagerForm(request.POST)
                form.handle(manager.id)
                return HttpResponseRedirect(request.path)
            except:
                return HttpResponse('Some ERROR occurs on the server! Please, try again.')
        else:
            try:
                project_user = ProjectManagerUser.objects.project_users(manager.id)
                if len(project_user)!= 0:
                    time_table = Time.take_time.unconfirmed(manager.id)
                    return render_to_response(template_manager, {'time_table': time_table,
                                    'manager_name': manager.username, 'project_user': project_user})
                else:
                    return HttpResponseRedirect(reverse('manager_crud'))
            except:
                return HttpResponse('Some ERROR occurs on the server! Please, try again.')
    else:
        return HttpResponseRedirect(reverse('invalid'))
        
        
def manager_crud(request, template_manager_crud='manager/manager_crud_timesheet.html'):
    if request.user.is_authenticated():
        manager = request.user
        if request.method == 'POST':
            try: 
                form = ProjectUserForm(request.POST)
                manager_id = ProjectManager.objects.get(manager=manager.id).id
                form.handle(manager_id)
                return HttpResponseRedirect(request.path)
            except:
                return HttpResponse('Some ERROR occurs on the server! Please, try again.')
        else:
            try:
                project = Project.take_project.all()
                user = ProjectManagerUser.objects.take_all_users()
                if len(project)!=0 and len(user)!=0:
                    his_project_user = ProjectManagerUser.objects.take_all_project_users(manager.id)
                    return render_to_response(template_manager_crud, {'manager_name': manager.username, 
                                    'project': project, 'user': user, 'his_project_user': his_project_user})
                else:
                    return HttpResponse("ERROR: there are empty user's or project's list")
            except:
                return HttpResponse('Some ERROR occurs on the server! Please, try again.')
    else:
        return HttpResponseRedirect(reverse('invalid'))
